package com.ruoyi.web.controller.admin.construction;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.dto.ItemAddDTO;
import com.ruoyi.system.dto.ItemUpdateDTO;
import com.ruoyi.system.dto.ItemCompleteDTO;
import com.ruoyi.system.dto.ProjectStageAddDTO;
import com.ruoyi.system.dto.convert.ProjectItemConverter;
import com.ruoyi.system.entity.ConstructionProjectItemDO;
import com.ruoyi.system.service.IConstructionItemService;
import com.ruoyi.system.service.IProjectStageService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 施工进度管理Controller
 */
@RestController
@RequestMapping("/admin/construction/progress")
public class ConstructionProgressController extends BaseController {

    @Resource
    private IProjectStageService projectStageService;

    @Resource
    private IConstructionItemService projectItemService;

    /**
     * 获取项目所有阶段
     */
    @GetMapping("/stages/{projectId}")
    public AjaxResult getStagesByProjectId(@PathVariable Long projectId) {
        return AjaxResult.success(projectStageService.getStagesByProjectId(projectId));
    }

    /**
     * 新增项目阶段
     */
    @PostMapping("/stage/add")
    public AjaxResult addStage(@Validated @RequestBody ProjectStageAddDTO stageAddDTO) {
        return AjaxResult.success(projectStageService.addStage(stageAddDTO));
    }

    /**
     * 删除项目阶段
     */
    @DeleteMapping("/stage/{projectId}/{stageId}")
    public AjaxResult deleteStage(@PathVariable Long projectId, @PathVariable Long stageId) {
        return AjaxResult.success(projectStageService.deleteStage(projectId, stageId));
    }

    /**
     * 查询施工事项详情
     */
    @GetMapping("/item/{itemId}")
    public AjaxResult getItemById(@PathVariable Long itemId) {
        ConstructionProjectItemDO item = projectItemService.getById(itemId);
        if (item == null) {
            return AjaxResult.error("事项不存在");
        }
        return AjaxResult.success(ProjectItemConverter.INSTANCE.doToDto(item));
    }

    /**
     * 获取项目阶段的所有事项
     */
    @GetMapping("/items/{projectId}/{stageId}")
    public AjaxResult getItemsByStageIdForStage(@PathVariable Long projectId,@PathVariable Long stageId) {
        return AjaxResult.success(projectItemService.getItemsByStageId(projectId, stageId));
    }

    /**
     * 新增施工事项
     */
    @PostMapping("/item/add")
    public AjaxResult addItem(@Validated @RequestBody ItemAddDTO itemAddDTO) {
        return AjaxResult.success(projectItemService.addItem(itemAddDTO));
    }

    /**
     * 修改施工事项
     */
    @PutMapping("/item/update")
    public AjaxResult updateItem(@Validated @RequestBody ItemUpdateDTO itemUpdateDTO) {
        return AjaxResult.success(projectItemService.updateItem(itemUpdateDTO));
    }

    /**
     * 删除施工事项
     */
    @DeleteMapping("/item/{itemId}")
    public AjaxResult deleteItem(@PathVariable Long itemId) {
        return AjaxResult.success(projectItemService.deleteItem(itemId));
    }

    /**
     * 手动完工施工事项
     */
    @PostMapping("/item/complete")
    public AjaxResult completeItem(@Validated @RequestBody ItemCompleteDTO itemCompleteDTO) {
        return AjaxResult.success(projectItemService.completeItem(itemCompleteDTO));
    }
}
